<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>单元测试 | Skyung-Blog</title>
  <meta name="description" content="断言   断言 功能    assertEquals(expected value, actual value) assertEquals([String message], expected value, actual value)   assertEquals(double expected value, double actual value, double delta) 检查指定误差范围内">
<meta property="og:type" content="article">
<meta property="og:title" content="单元测试">
<meta property="og:url" content="https://skyung.gitee.io/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/index.html">
<meta property="og:site_name" content="Skyung-Blog">
<meta property="og:description" content="断言   断言 功能    assertEquals(expected value, actual value) assertEquals([String message], expected value, actual value)   assertEquals(double expected value, double actual value, double delta) 检查指定误差范围内">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-05-06T16:00:00.000Z">
<meta property="article:modified_time" content="2022-05-06T16:00:00.000Z">
<meta property="article:author" content="Skyung">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="https://skyung.gitee.io/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/index.html">
  
    <link rel="alternate" href="/atom.xml" title="Skyung-Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="images/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/Skyung/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.1"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/cofess" target="_blank">
          <img class="img-circle img-rotate" src="/Skyung/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">Skyung</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Web Developer</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Wuhan, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav ">
        
        
        <li class="menu-item menu-item-home">
          <a href="/Skyung/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/Skyung/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/Skyung/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/Skyung/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/Skyung/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-books">
          <a href="/Skyung/books">
            
            <i class="icon icon-book-fill"></i>
            
            <span class="menu-title">书单</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/Skyung/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">友链</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/Skyung/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="/Skyung/null" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/Skyung/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a><span class="category-list-count">20</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/java/" rel="tag">java</a><span class="tag-list-count">19</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/jvm/" rel="tag">jvm</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/linux/" rel="tag">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/redis/" rel="tag">redis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" rel="tag">多线程</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/" rel="tag">并发编程</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag">设计模式</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/Skyung/tags/%E9%9B%B6%E6%95%A3%E7%82%B9/" rel="tag">零散点</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/Skyung/tags/java/" style="font-size: 14px;">java</a> <a href="/Skyung/tags/jvm/" style="font-size: 13px;">jvm</a> <a href="/Skyung/tags/linux/" style="font-size: 13px;">linux</a> <a href="/Skyung/tags/redis/" style="font-size: 13px;">redis</a> <a href="/Skyung/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" style="font-size: 13px;">多线程</a> <a href="/Skyung/tags/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/" style="font-size: 13.33px;">并发编程</a> <a href="/Skyung/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" style="font-size: 13.67px;">设计模式</a> <a href="/Skyung/tags/%E9%9B%B6%E6%95%A3%E7%82%B9/" style="font-size: 13px;">零散点</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/Skyung/archives/2022/05/">五月 2022</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/Skyung/archives/2022/04/">四月 2022</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/Skyung/archives/2022/03/">三月 2022</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/Skyung/archives/2022/02/">二月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/Skyung/archives/2021/09/">九月 2021</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
              </p>
              <p class="item-title">
                <a href="/Skyung/2022/05/08/Spring/" class="title">spring</a>
              </p>
              <p class="item-date">
                <time datetime="2022-05-07T16:00:00.000Z" itemprop="datePublished">2022-05-08</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
              </p>
              <p class="item-title">
                <a href="/Skyung/2022/05/08/netty-RPC/" class="title">Netty-RPC</a>
              </p>
              <p class="item-date">
                <time datetime="2022-05-07T16:00:00.000Z" itemprop="datePublished">2022-05-08</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
              </p>
              <p class="item-title">
                <a href="/Skyung/2022/05/07/%E5%B8%B8%E7%94%A8%E7%B1%BB%E5%BA%93%E5%92%8CAPI/" class="title">常见类库</a>
              </p>
              <p class="item-date">
                <time datetime="2022-05-06T16:00:00.000Z" itemprop="datePublished">2022-05-07</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
              </p>
              <p class="item-title">
                <a href="/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" class="title">单元测试</a>
              </p>
              <p class="item-date">
                <time datetime="2022-05-06T16:00:00.000Z" itemprop="datePublished">2022-05-07</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
              </p>
              <p class="item-title">
                <a href="/Skyung/2022/05/01/netty%E8%81%8A%E5%A4%A9%E5%AE%A4/" class="title">Netty聊天室</a>
              </p>
              <p class="item-date">
                <time datetime="2022-04-30T16:00:00.000Z" itemprop="datePublished">2022-05-01</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
  <aside class="sidebar sidebar-toc collapse   in  " id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%AD%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">断言</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-assertThat-%E5%92%8C-Matcher"><span class="toc-number">1.1.</span> <span class="toc-text">使用 assertThat 和 Matcher</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E5%BC%82%E5%B8%B8"><span class="toc-number">2.</span> <span class="toc-text">测试异常</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-Test-%E6%B3%A8%E8%A7%A3%E4%B8%AD%E7%9A%84-expected"><span class="toc-number">2.1.</span> <span class="toc-text">使用 @Test 注解中的 expected</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-assertThrows-%E6%96%AD%E8%A8%80%E5%B7%A5%E5%85%B7"><span class="toc-number">2.2.</span> <span class="toc-text">使用 assertThrows 断言工具</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#try-catch-%E6%B5%8B%E8%AF%95%E5%BC%82%E5%B8%B8"><span class="toc-number">2.3.</span> <span class="toc-text">try&#x2F;catch 测试异常</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E8%A7%A3"><span class="toc-number">3.</span> <span class="toc-text">注解</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E6%95%B0%E5%8C%96%E7%94%A8%E4%BE%8B"><span class="toc-number">4.</span> <span class="toc-text">参数化用例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MOCK"><span class="toc-number">5.</span> <span class="toc-text">MOCK</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E8%A1%8C%E4%B8%BA"><span class="toc-number">5.1.</span> <span class="toc-text">基于行为</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E7%8A%B6%E6%80%81"><span class="toc-number">5.2.</span> <span class="toc-text">基于状态</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E6%8A%80%E5%B7%A7"><span class="toc-number">6.</span> <span class="toc-text">其他技巧</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%B9%E9%87%8F%E6%B5%8B%E8%AF%95"><span class="toc-number">6.1.</span> <span class="toc-text">批量测试</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Junit-Rule-%E4%BD%BF%E7%94%A8"><span class="toc-number">6.2.</span> <span class="toc-text">Junit Rule 使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%A6%86%E7%9B%96%E7%8E%87"><span class="toc-number">6.3.</span> <span class="toc-text">测试覆盖率</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%B6%85%E6%97%B6"><span class="toc-number">6.4.</span> <span class="toc-text">测试超时</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BF%BD%E7%95%A5%E6%B5%8B%E8%AF%95"><span class="toc-number">6.5.</span> <span class="toc-text">忽略测试</span></a></li></ol></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-单元测试" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      单元测试
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" class="article-date">
	  <time datetime="2022-05-06T16:00:00.000Z" itemprop="datePublished">2022-05-07</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/Skyung/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/Skyung/tags/java/" rel="tag">java</a>
  </span>


        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h3 id="断言"><a href="#断言" class="headerlink" title="断言"></a>断言</h3><table>
<thead>
<tr>
<th>断言</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>assertEquals(expected value, actual value)</td>
<td>assertEquals([String message], expected value, actual value)</td>
</tr>
<tr>
<td>assertEquals(double expected value, double actual value, <strong>double delta</strong>)</td>
<td>检查<strong>指定误差范围内的double类型值是否相等</strong>，需要第三个参数delta表示可接受的误差范围</td>
</tr>
<tr>
<td>assertNotEquals(expected value, actual value)</td>
<td>检查<strong>值是否不相等</strong>，第一个参数是一个可选的字符串消息。</td>
</tr>
<tr>
<td>assertArrayEquals(expectedArray, resultArray)</td>
<td>检查<strong>两数组内容是否相同</strong></td>
</tr>
<tr>
<td>assertTrue(boolean condition)</td>
<td>检查<strong>条件是否为真</strong></td>
</tr>
<tr>
<td>assertFalse(boolean condition)</td>
<td>检查<strong>条件是否为假</strong></td>
</tr>
<tr>
<td>assertNull(Object object)</td>
<td></td>
</tr>
<tr>
<td>assertNotNull(Object object)</td>
<td></td>
</tr>
<tr>
<td>assertSame(Object expected, Object actual)</td>
<td></td>
</tr>
<tr>
<td>assertNotSame(Object expected, Object actual)</td>
<td></td>
</tr>
</tbody></table>
<h4 id="使用-assertThat-和-Matcher"><a href="#使用-assertThat-和-Matcher" class="headerlink" title="使用 assertThat 和 Matcher"></a>使用 assertThat 和 Matcher</h4><p>上面的几种断言方法，还不能满足所有需求，比如想要验证一个返回值是否是某种类型就无法办到，除非使用 assertTrue，并配合自己的逻辑。</p>
<p>为了让断言更为丰富，Junit 使用了更为高明的方法，它提供一个 assertThat 方法 (实际上是 hamcrest 提供的)，第一个方法为，第二个方法接受一个函数，这个函数只用来计算条件是否满足。这个方法一般叫做 Matcher，Matcher 可以由第三方库提供。</p>
<p>java 中比较著名的 Matcher 库有 hamcrest 和 AssertJ，hamcrest 为 junit 内置的库，大部分情况实际上够用了。</p>
<p>例如，编写了一个 helloAndNow 方法返回 “Hello, world!” 以及当前的时间。现在没有办法使用 assertEquals，可以使用 assertThat 和 startsWith matcher 。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">helloAndNow</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Hello, world!&quot;</span> + System.currentTimeMillis();</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">should_start_with_hello</span><span class="params">()</span> &#123;</span><br><span class="line">    assertThat(helloAndNow(), startsWith(<span class="string">&quot;Hello&quot;</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>下面是一些常用的 matcher，其中几个逻辑 matcher 非常实用，可以大大减少测试的代码量。</p>
<p>Matcher示例用途anythingassertThat(“hamcrest”,anything());任何内容,一般只用来占位使用describedAsassertThat(“hamcrest”, describedAs(“a description”, anything()));用来包装一个描信息isassertThat(“hamcrest”, is((anything()));其实没有什么用，包装一个语义信息allOfassertThat(“hamcrest”, allOf(anything(), anything(), anything()));串联多个断言anyOfassertThat(“hamcrest”, anyOf(anything(), anything(), anything()));并联多个断言notassertThat(“hamcrest”, not(not(anything())));取反断言equalToassertThat(“hamcrest”, equalTo(“hamcrest”));测试对象 equals 相等instanceOfassertThat(“hamcrest”, instanceOf(String.class));测试对象类型notNullValue,nullValueassertThat(null,nullValue());测试空、非空值sameInstanceassertThat(Runtime.getRuntime(), sameInstance(Runtime.getRuntime()));测试对象引用是否一致hasItemsassertThat(Arrays.asList(1,2,3), hasItems(1, 2));断言列表内容</p>
<p>如果这些断言仍然无法满足你的需求，也可以编写一个自己的 matcher</p>
<h3 id="测试异常"><a href="#测试异常" class="headerlink" title="测试异常"></a>测试异常</h3><p>对异常断言有几种方式，@Test 注解中的 expected、assertThrows 断言工具、使用 Rule 等几种方式</p>
<h4 id="使用-Test-注解中的-expected"><a href="#使用-Test-注解中的-expected" class="headerlink" title="使用 @Test 注解中的 expected"></a>使用 @Test 注解中的 expected</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test(expected = DivideByZeroException.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">should_get_error_when_divisor_is_zero</span><span class="params">()</span> &#123;</span><br><span class="line">    assertEquals(<span class="number">3</span>, divide(<span class="number">9</span>, <span class="number">0</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="使用-assertThrows-断言工具"><a href="#使用-assertThrows-断言工具" class="headerlink" title="使用 assertThrows 断言工具"></a>使用 assertThrows 断言工具</h4><p>给丢出异常的地方加上消息。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">DivideByZeroException</span>(<span class="string">&quot;divisor is zero&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>接下来，使用 assertThrows 断言异常以及消息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">should_assert_exception_type_and_message</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">DivideByZeroException</span> <span class="variable">divideByZeroException</span> <span class="operator">=</span> assertThrows(DivideByZeroException.class, () -&gt; divide(<span class="number">9</span>, <span class="number">0</span>));</span><br><span class="line">    assertEquals(<span class="string">&quot;divisor is zero&quot;</span>,divideByZeroException.getMessage());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="try-catch-测试异常"><a href="#try-catch-测试异常" class="headerlink" title="try/catch 测试异常"></a>try/catch 测试异常</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test_exception_by_try_catch</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        divide(<span class="number">9</span>, <span class="number">0</span>);</span><br><span class="line">        fail(<span class="string">&quot;Expected DivideByZeroException be thrown&quot;</span>);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (DivideByZeroException divideByZeroException) &#123;</span><br><span class="line">        assertThat(divideByZeroException.getMessage(), is(<span class="string">&quot;divisor is zero&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="注解"><a href="#注解" class="headerlink" title="注解"></a>注解</h3><p>Junit 4 中有四个执行过程注解：</p>
<ul>
<li>@BeforeClass 用于类加载首次执行，必须使用在静态方法上</li>
<li>@Before 用于实例方法，在每个测试用例之前执行</li>
<li>@After 用于实例方法，在每个测试用例之间执行</li>
<li>@AfterClass 用于类完成执行，必须使用在静态方法上</li>
</ul>
<p>在 @Before 和 @After 方法之间，执行每一个测试用例。他们的执行顺序是：</p>
<blockquote>
<p>@BeforeClass -&gt; @Before -&gt; @After -&gt; @Test -&gt; @AfterClass</p>
</blockquote>
<h3 id="参数化用例"><a href="#参数化用例" class="headerlink" title="参数化用例"></a>参数化用例</h3><p>共有 6 中输入数据。每一个行对应一个单元测试，会非常冗长。</p>
<p>可以使用 Parameterized 的风格输入一组二位数组。具体用法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(Parameterized.class)</span><span class="comment">//加注解</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ParameterizedPractiseTest</span> &#123; </span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> input1;<span class="comment">//储存输入参数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> input2;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> expect;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//构造数据集</span></span><br><span class="line">    <span class="meta">@Parameterized</span>.Parameters</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Collection&lt;Object[]&gt; data() &#123;</span><br><span class="line">        <span class="keyword">return</span> Arrays.asList(<span class="keyword">new</span> <span class="title class_">Object</span>[][]&#123;</span><br><span class="line">            &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;,&#123;<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>&#125;,&#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>&#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">    <span class="comment">//接收数据</span></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">ParameterizedPractiseTest</span><span class="params">(<span class="type">int</span> input1, <span class="type">int</span> input2, <span class="type">int</span> expect)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.input1 = input1;</span><br><span class="line">        <span class="built_in">this</span>.input2 = input2;</span><br><span class="line">        <span class="built_in">this</span>.expect = expect;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//given</span></span><br><span class="line">        <span class="type">Calculate</span> <span class="variable">calculate</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Calculate</span>();</span><br><span class="line">        <span class="comment">//when</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">actual</span> <span class="operator">=</span> calculate.add(input1, input2);</span><br><span class="line">        Assert.assertEquals(actual, expect);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>实际上这只是一种语法糖，通过 @Runwith 注解输入了 Parameterized Runner 帮助生成了6 个独立的测试。</p>
<p>@Parameterized.Parameters 定义了 6 条数据作为输入输出。这个注解允许传入一个模板，给6个独立的测试输出一个名称，便于识别。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Parameterized</span>.Parameters(name = <span class="string">&quot;&#123;index&#125;_input_(&#123;0&#125;)_should_be_&#123;1&#125;&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="MOCK"><a href="#MOCK" class="headerlink" title="MOCK"></a>MOCK</h3><p>对于某些不容易构造(如HttpServletRequest必须在Servlet容器中才能构造出来)或者不容易获取比较复杂的对象(如JDBC中的ResultSet对象) ,用一个虚拟的对象(Mock 对象)来创建，以便测试方便。</p>
<ul>
<li>解决不同单元之间耦合难以测试的问题</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.jmockit&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;jmockit&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;<span class="number">1.8</span>&lt;/version&gt;</span><br><span class="line">    &lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<p>JMockit有两种测试方式，一种是基于行为的，一种是基于状态的测试</p>
<h4 id="基于行为"><a href="#基于行为" class="headerlink" title="基于行为"></a>基于行为</h4><p>基于行为的方式去Mock类的方法，需要对Mocked类加上@Mocked注解，Mock的行为定义在Expectations对象中，这里对getLibraryInfo方法的Mock方式进行了定义。 其他未定义的方法采用默认的Mock方式进行Mock，<strong>默认Mock方法result=nul</strong>l。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BookServiceTest</span> &#123;</span><br><span class="line">    <span class="meta">@Mocked</span></span><br><span class="line">    BookService bookService;</span><br><span class="line">    <span class="meta">@Test</span> </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testGetLibraryInfo</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">// 录制，定义被模拟的方法的返回值，与被调用的次数</span></span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Expectations</span>()&#123;&#123; </span><br><span class="line">            bookService.getLibraryInfo();</span><br><span class="line">            result = <span class="string">&quot;hello library&quot;</span>;</span><br><span class="line">            times = <span class="number">2</span>; </span><br><span class="line">        &#125;&#125;</span><br><span class="line">        <span class="comment">// 回放，调用被模拟的方法 </span></span><br><span class="line">        System.out.println(bookService.getLibraryInfo());</span><br><span class="line">        System.out.println(<span class="keyword">new</span> <span class="title class_">BookService</span>().getLibraryInfo());</span><br><span class="line">        System.out.println(bookService.displayBook(<span class="keyword">new</span> <span class="title class_">Book</span>()));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//运行结果为</span></span><br><span class="line">hello library</span><br><span class="line">hello library</span><br><span class="line"><span class="literal">null</span></span><br></pre></td></tr></table></figure>

<h4 id="基于状态"><a href="#基于状态" class="headerlink" title="基于状态"></a>基于状态</h4><p>基于状态的方法是用MockUp类去实现，直接覆盖待Mock类的方法的实现，未覆盖的方法不做Mock。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BookServiceTest0</span> &#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testGetLibraryInfo</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">MockUp</span>&lt;BookService&gt;(BookService.class) &#123;</span><br><span class="line">            <span class="meta">@Mock</span></span><br><span class="line">            <span class="keyword">public</span> String <span class="title function_">getLibraryInfo</span><span class="params">()</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">&quot;Hello library!&quot;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        System.out.println(<span class="keyword">new</span> <span class="title class_">BookService</span>().getLibraryInfo());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="其他技巧"><a href="#其他技巧" class="headerlink" title="其他技巧"></a>其他技巧</h3><h4 id="批量测试"><a href="#批量测试" class="headerlink" title="批量测试"></a>批量测试</h4><p>有时候需要将一组测试用例一起作为一个测试套件运行。可以使用 Suite 作为 Runner 实现，通过 @Suite.SuiteClasses 传入需要组合的测试类即可实现单元测试套件。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(Suite.class)</span></span><br><span class="line"><span class="meta">@Suite</span>.SuiteClasses(&#123;</span><br><span class="line">        HelloWorldTest.class,</span><br><span class="line">        AssertPractiseTest.class</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SuitesPractiseTest</span> &#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="Junit-Rule-使用"><a href="#Junit-Rule-使用" class="headerlink" title="Junit Rule 使用"></a>Junit Rule 使用</h4><p>Junit Rule 提供了一种类似拦截器的机制，用于拓展 junit 的各种行为，给每个测试添加一些通用的行为。常见的用途有全局设置超时时间、获取当前的测试名称、打印日志等。</p>
<p>一个最简单使用 rule 的例子，TestName 会被每个测试在运行期执行，并将当前的信息传递给 rule，通过这种方式获得测试名称。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">NameRuleTest</span> &#123;</span><br><span class="line">  <span class="meta">@Rule</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">final</span> <span class="type">TestName</span> <span class="variable">name</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TestName</span>();</span><br><span class="line">  </span><br><span class="line">  <span class="meta">@Test</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testA</span><span class="params">()</span> &#123;</span><br><span class="line">    assertEquals(<span class="string">&quot;testA&quot;</span>, name.getMethodName());</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="meta">@Test</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testB</span><span class="params">()</span> &#123;</span><br><span class="line">    assertEquals(<span class="string">&quot;testB&quot;</span>, name.getMethodName());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="测试覆盖率"><a href="#测试覆盖率" class="headerlink" title="测试覆盖率"></a>测试覆盖率</h4><p>如果我们的用例设计的不好，或者不够多，必然很多分支和情况没有考虑到。我们可以通过统计测试覆盖率和覆盖情况检查我们用例设计，从而改善用例设计。</p>
<p>使用 IDE 内置的测试覆盖率统计统计，即可满足我们大部分需求。只需要使用 Run xx with Coverage 即可。Intellij Idea 不仅可以针对类统计覆盖率，还可以选中一个包统计单元测试覆盖率。</p>
<h4 id="测试超时"><a href="#测试超时" class="headerlink" title="测试超时"></a>测试超时</h4><p>如果一个测试运行时间很长，往往意味着测试失败。junit 提供了一种方式结束它，使用 @Test 注解的 timeout 参数可以传入一个毫秒数。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span>（timeout = <span class="number">1000</span>）</span><br><span class="line">    <span class="keyword">public</span>  <span class="keyword">void</span> test_with_timeout（）&#123;</span><br><span class="line">    ... </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="忽略测试"><a href="#忽略测试" class="headerlink" title="忽略测试"></a>忽略测试</h4><p>如果因为某些原因（可能是快速修复 CI，避免影响队友工作），你需要快速忽略测试。尽量不要使用大面积注释代码的方法，请使用 @Ignore 注解，并加上原因，并随后修复。</p>
<p>和删除/注释代码相比， @Ignore 会被测试框架统计进去，显示为已经忽略的测试数量。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Ignore(&quot;Test is ignored as a demonstration&quot;)</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">assert_same</span><span class="params">()</span> &#123;</span><br><span class="line">    assertThat(<span class="number">1</span>, is(<span class="number">1</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://skyung.gitee.io/Skyung/2022/05/07/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" title="单元测试" target="_blank" rel="external">https://skyung.gitee.io/Skyung/2022/05/07/单元测试/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/cofess" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/Skyung/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/cofess" target="_blank"><span class="text-dark">Skyung</span><small class="ml-1x">Web Developer</small></a></h3>
        <div>脚踏实地，仰望星空</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="uyan_frame"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/Skyung/2022/05/07/%E5%B8%B8%E7%94%A8%E7%B1%BB%E5%BA%93%E5%92%8CAPI/" title="常见类库"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/Skyung/2022/05/01/netty%E8%81%8A%E5%A4%A9%E5%AE%A4/" title="Netty聊天室"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn " data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">    <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>赏</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>感谢您的支持，我会继续努力的!</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="/Skyung/images/donate/alipay.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开支付宝扫一扫，即可进行扫码打赏哦</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="/Skyung/images/donate/wechat.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开微信扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> 支付宝</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> 微信支付</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="/Skyung/null" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="/Skyung/null" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/Skyung/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/Skyung/js/plugin.min.js"></script>


<script src="/Skyung/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/Skyung/',
        CONTENT_URL: '/Skyung/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/Skyung/js/insight.js"></script>






   




   
    
    <script defer type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=[object Object]"></script>








</body>
</html>